package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.AddUnitReportActivityDelegate
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.moudel.bean.AddResourceObj
import cc.vv.btpartyjob.partymanager.moudel.bean.AddSubmitResourceObj
import cc.vv.btpartyjob.partymanager.moudel.req.SubmitUnitReportRequestObj
import cc.vv.btpartyjob.partymanager.moudel.resp.SubmitUnitReportResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 单位报到详情
 * Created by Chen.qi on 2018/10/27.
 */
class AddUnitReportActivity : BaseActivityMVP<AddUnitReportActivityDelegate>() {
    /**
     * 资源集合
     */
    private var resourceList: ArrayList<AddResourceObj>? = null
    /**
     * 新增资源集合
     */
    private var newL: ArrayList<AddSubmitResourceObj>? = null
    /**
     * 单位id
     */
    private var unitId: String? = null

    /**
     * 街道id
     */
    private var jieDaoId: String? = null

    private var jidDaoName: String? = null

    private var sheQuId: String? = null

    private var sheQuName: String? = null

    private var danWeiName: String? = null

    private var path: String? = null

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        viewDelegate?.setTopBarTitle("添加单位报到")
        resourceList = ArrayList()
        newL = ArrayList()

        val userInfo = UserInfoManageUtil.getUserInfo()
        if (TextUtils.isEmpty(unitId)) {
            val userOrgList = userInfo?.userOrgList
            if (null != userOrgList) {
                for (i in userOrgList.indices) {
                    if (userOrgList[i].type == 4) {
                        unitId = userOrgList[i].id
                        path = userOrgList[i].path
                        danWeiName = userOrgList[i].name
                        break
                    }
                }
            }
        }


        if (!TextUtils.isEmpty(path)) {
            val split = path!!.split("-")
            val jd = split[1]
            val sq = split[2]
            jieDaoId = jd
            sheQuId = sq
            for (i in 0 until userInfo?.userOrgList?.size!!) {
                val userOrgList = userInfo.userOrgList[i]
                if (jd == userOrgList?.id) {
                    jidDaoName = userOrgList.name
                } else if (sq == userOrgList?.id) {
                    sheQuName = userOrgList.name
                }
            }
        }

        viewDelegate?.setReportTime()
        viewDelegate?.setStreetName(jidDaoName)
        viewDelegate?.setCommunityName(sheQuName)
        viewDelegate?.setUnitName(danWeiName)

    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {

            }

            override fun rightOnClick() {

            }
        })

//        viewDelegate?.setOnClickListener(View.OnClickListener {
//            //报到街道
//            val intent = Intent(this@AddUnitReportActivity, OrganizationActivity::class.java)
//            intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 1)
//            intent.putExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 1)
//            startActivityForResult(intent, 100)
//        }, R.id.ll_auRA_jiedao)
//
//        viewDelegate?.setOnClickListener(View.OnClickListener {
//            //报到社区
//            if (!street) {
//                LKToastUtil.showToastShort("请先选择街道")
//                return@OnClickListener
//            }
//            val intent = Intent(this@AddUnitReportActivity, JLOrganizationActivity::class.java)
//            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_ID, jieDaoId)
//            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_TYPE, "2")
//            startActivityForResult(intent, 101)
//
//        }, R.id.ll_auRA_shequ)
//
//        viewDelegate?.setOnClickListener(View.OnClickListener {
//            //报到单位
//            if (!shequ) {
//                LKToastUtil.showToastShort("请先选择社区")
//                return@OnClickListener
//            }
//            val intent = Intent(this@AddUnitReportActivity, JLOrganizationActivity::class.java)
//            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_ID, sheQuId)
//            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_TYPE, "4")
//            startActivityForResult(intent, 102)
//
//        }, R.id.ll_auRA_danwei)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //新增资源
            val intent = Intent(this, AddResourceActivity::class.java)
            startActivityForResult(intent, 103)
        }, R.id.rl_auRA_addResource)


        viewDelegate?.setOnClickListener(View.OnClickListener {

            val unitName = danWeiName
            val principalName = viewDelegate?.getPrincipalName()
            val mobile = viewDelegate?.getMobile()
            val partyPerNum = viewDelegate?.getPartyPerNum()
            val unitAddress = viewDelegate?.getUnitAddress()
            val unitCode = viewDelegate?.getUnitCode()

            if (TextUtils.isEmpty(unitName) || TextUtils.isEmpty(principalName) || TextUtils.isEmpty(partyPerNum)
                    || TextUtils.isEmpty(unitAddress) || TextUtils.isEmpty(unitCode) || TextUtils.isEmpty(mobile)) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }

            val request = SubmitUnitReportRequestObj()
            request.unitName = unitName
            request.principal = principalName
            request.partyNum = partyPerNum
            request.unitAddr = unitAddress
            request.mobile = mobile
            request.zipCode = unitCode
            request.street = jieDaoId
            request.community = sheQuId
            request.unitId = unitId
            request.reportTime = System.currentTimeMillis()
            if (newL!!.size > 0) {
                request.resourceList = newL
            }
            //提交
            PartyManagerServer.submitUnitReport(request)
        }, R.id.rl_auRA_submit)

    }

    override fun getDelegateClass(): Class<AddUnitReportActivityDelegate> {
        return AddUnitReportActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is SubmitUnitReportResponseObj -> {
                val intent = Intent(this, UnitReportDetailActivity::class.java)
                intent.putExtra("unitId", unitId)
                startActivity(intent)
                finish()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (requestCode == 103 && resultCode == 101) {
            val resourceObj = data.getSerializableExtra("ADD_RESOURCE") as AddResourceObj
            val subRe = AddSubmitResourceObj()
            val longTime = LKTimeUtil.getInstance().getLongTime(resourceObj.startTime, "yyyy-MM-dd")
            val endTime = LKTimeUtil.getInstance().getLongTime(resourceObj.endTime, "yyyy-MM-dd")
            subRe.startTime = longTime
            subRe.endTime = endTime
            subRe.amount = resourceObj.amount
            subRe.conetnt = resourceObj.conetnt
            subRe.amount = resourceObj.amount
            subRe.reward = resourceObj.reward
            subRe.unitId = unitId
            subRe.resourceType = resourceObj.resourceType
            newL!!.add(subRe)
            resourceList?.add(resourceObj)
            viewDelegate?.setListData(resourceList)
        }

    }
}